#include "Model.h"

namespace Render
{
	bool Model::setData(const Resource::Mesh *mesh)
	{
		ASSERT(mesh);
		
		memcpy(&m_mesh, mesh, sizeof(Resource::MeshHead));
		
		glBindVertexArray(m_vao);
		
		glBindBuffer(GL_ARRAY_BUFFER, m_vbo[0]);
		glBufferData(GL_ARRAY_BUFFER, m_mesh.head.vcount * sizeof(Vertex), GL_PVOID(mesh->vdata), GL_STATIC_DRAW);
		
		glVertexAttribPointer(VERT_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), GL_PVOID(0));
		glEnableVertexAttribArray(VERT_POSITION);
		
		glVertexAttribPointer(VERT_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), GL_PVOID(sizeof(float3)));
		glEnableVertexAttribArray(VERT_NORMAL);
		
		glVertexAttribPointer(VERT_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), GL_PVOID(sizeof(float3) * 2));
		glEnableVertexAttribArray(VERT_TEXCOORD);
		
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbo[1]);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_mesh.head.icount * sizeof(uint32_t), GL_PVOID(mesh->idata), GL_STATIC_DRAW);
		
		return true;
	}
}
